/**
 * Copyright 2021 Inc.
 **/
package com.myz.distributed.transaction.rocketmq.common;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * @author maoyz0621 on 2021/5/21
 * @version v1.0
 */
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {

    /**
     * JPS 更新操作 @Modifying
     */
    @Modifying
    @Query(value = "update user1 set money = money - ?2 where id = ?1 and money >= ?2", nativeQuery = true)
    int reduceMoney(Long fromUserId, Long changeMoney);

    @Modifying
    @Query(value = "update user1 set money = money + ?2 where id = ?1 and money >= ?2", nativeQuery = true)
    int increaseMoney(Long fromUserId, Long changeMoney);
}